<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>欢迎注册</title>
  <link href="../css/register.css" rel="stylesheet">
</head>
<body>
<div class="form-div">
  <div class="reg-content">
    <h1>欢迎注册</h1>
    <span>已有帐号？</span> <a href="#">登录</a>
  </div>
  <form id="reg-form" action="#" method="get">
    <!--注册规则:
        校验用户名: 当用户输入框失去焦点时,判断输入的内容是否符合规则(长度: 6-12)
        校验密码: 判断输入的内容是否符合规则(长度: 6-12)
        校验手机号:判断输入的内容是否符合规则(长度: 11)
    -->
    <table>
      <tr>
        <td>用户名</td>
        <td class="inputs">
          <input name="username" type="text" id="username">
          <br>
          <span id="username_err" class="err_msg" style="display: none">用户名长度为6~12</span>
        </td>
      </tr>

      <tr>
        <td>密码</td>
        <td class="inputs">
          <input name="password" type="password" id="password">
          <br>
          <span id="password_err" class="err_msg" style="display: none">密码是字母和数字组成，长度3-6位</span>
        </td>
      </tr>

      <tr>
        <td>手机号</td>
        <td class="inputs"><input name="tel" type="text" id="tel">
          <br>
          <span id="tel_err" class="err_msg" style="display: none">手机号格式有误</span>
        </td>
      </tr>
    </table>
    <div class="buttons">
      <input value="注 册" type="submit" id="reg_btn">
    </div>
    <br class="clear">
  </form>

</div>

<!--JS代码 -->
<script>
  //一、校验用户名是否符合规则
  //获取用户名的输入框
  var usernameInput = document.getElementById("username");
  //绑定事件 - onblur
  // usernameInput.onblur =function () {
  //   //获取用户输入的用户名 - value
  //   var username = usernameInput.value.trim();
  //   //判断用户名是否符合规则:长度6~12
  //   if (username.length >=6 && username.length <= 12){
  //     document.getElementById("username_err").style.display='none';
  //   }else {
  //     document.getElementById("username_err").style.display = '请输入6-12位的账号';
  //   }
  // }
  usernameInput.onblur = checkName;
  function checkName() {

    var username = usernameInput.value.trim();

    var flag = username.length >= 6 && username.length <= 12;
    if (flag) {
      document.getElementById("username_err").style.display = 'none';
    } else {
      //不符合规则
      document.getElementById("username_err").style.display = '';
    }
    return flag;
  }


  //二、校验密码是否符合规则
  var passwordInput = document.getElementById("password");

  passwordInput.onblur = checkPassword;

  function checkPassword(){
    var password = passwordInput.value.trim();
    var regx=/^[a-zA-Z0-9]{3,6}$/  //密码是字母和数字组成，长度3-6位
    var flag = regx.test(password);
    if (flag) {
      document.getElementById("password_err").style.display = 'none';
    } else {
      //不符合规则
      document.getElementById("password_err").style.display = '';
    }
    return flag;
  }
  //三、校验手机号码是否符合规则  == 11
  var telInput = document.getElementById("tel");
  telInput.onblur = checkPhone();
  function checkPhone() {
    var tel = telInput.value.trim();
    var regx = /^1[3|5|8|7][0-9]{9}$/;
    var flag = regx.test(tel);
    if (flag) {
      document.getElementById("password_err").style.display = 'none';
    } else {
      //不符合规则
      document.getElementById("password_err").style.display = '';
    }
    return flag;
  }
  //四、验证表单
  //1.获取表单对象
  var regForm = document.getElementById("reg-form");
  //2.绑定事件 - onsubmit
  regForm.onsubmit = function (){
    var usernameflag=checkName();
    var passwordflag=checkPassword();
    var phoneflag=checkPhone();
    return usernameflag && passwordflag && phoneflag;
  }



</script>

</body>
</html>